<?php
use think\facade\Session;
use think\facade\cookie;
use think\facade\Db;
use app\store\controller\Index;

/**
 * 管理员操作日志
 * @param string $type 操作类型 login add edit view delete
 * @param int    $param_id 操作类型
 * @param array  $param 提交的参数
 * @param subject  $param  操作主题add_log('upload', $data['user_id'], $data,'文件');
 */
function add_user_log($type, $subject='', $param_id = 0, $param = [])
{
  $action = '未知操作';
  $type_action = get_config('log.get_action');
   if($type_action[$type]){ 
        $action = $type_action[$type];
    }
     $login_user = Db::name('User')->where(['token' => cookie('userToken')])->find();
    $data = array();
    $data['param_id'] = isset(explode("=", substr(request()->url(), 0, 1500))[1])?explode("=", substr(request()->url(), 0, 1500))[1]:'0';
    if (empty(UserId())){
    $data['username'] = $login_user['username']??Index::cars()['car']; 
    }else{
    $data['username'] = UserId();
    }
    $data['url'] = substr(request()->url(), 0, 1500);
  
    $data['type'] = $type;
    $data['action'] = $action;
    
    if(empty($subject)){
            $data['subject'] = '系统';
        }else{
            $data['subject'] = $subject;
        }
 
    $data['content'] = $data['username'] . ', ' . '在 ' . date('Y-m-d H:i:s') . ' ' . $data['action'] . '了' . $data['subject'];
  
        if ($type == 'doSign' || $type == 'signature'){
          
    $data['content'] = $data['username'] . ', ' . '在 ' . date('Y-m-d H:i:s') . ' 执行了' . $data['action'] . '操作';  
        }
    $data['ip'] = app('request')->ip();
    $data['param'] = json_encode($param);
    $data['create_time'] = time();
   \app\lnadmin\model\admin\UserLog::insert($data);
}